using DTFramework.UI;
//using Sirenix.OdinInspector;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using Object = UnityEngine.Object;

namespace DTFramework
{
    [Serializable]
    public class BuiltinConfig : ScriptableObject
    {
        [Header("基础配置")]
        [Header("项目所属程序集")]
        public string m_ProjectAssembly = "Assembly-CSharp";
        [Header("项目所属入口")]
        public string m_ProjectProcedureEntry = "ProcedureEntry";
        //  [Header("Scriptable 名称数组")]
        ////  [InlineButton("AddScriptableName", "Add SOName")]
        //  public List<string> m_ScriptableNames;
        //  [Header("数据表 名称数组")]
        ////  [InlineButton("AddDataTableName", "Add DTName")]
        //  public List<string> m_DataTableNames;

        [Header("文件配置")]
        [Header("版本构建文件配置")]
        public TextAsset m_BuildInfoTextAsset;
        [Header("常用的UI预制体")]
        // [InlineButton("ReflashReference", "Reflash")]
        public UILoadingForm m_UILoadingFormTemplate;//进度条
        public NativeDialogForm m_NativeDialogFormTemplate;//对话框
        public UILoginForm m_UILoginFormTemplate;//登录
        public UICheckRunEnvirForm m_UICheckRunEnvirFormTemplate;//检查运行环境

        [Header("其他")]
        [Header("时间戳设置")]
        public TimeStampSetting m_Setting;

# if UNITY_EDITOR
        public void ReflashReference()
        {
            AddReferenceTextAsset(ref m_BuildInfoTextAsset, "BuildInfo");
            AddReferenceUIPrefab(ref m_UILoadingFormTemplate);
            AddReferenceUIPrefab(ref m_NativeDialogFormTemplate);
            AddReferenceUIPrefab(ref m_UILoginFormTemplate);
            AddReferenceUIPrefab(ref m_UICheckRunEnvirFormTemplate);
        }

        private void AddReferenceTextAsset(ref TextAsset target, string name)
        {
            target = (TextAsset)UnityEditor.AssetDatabase.LoadAssetAtPath(GetTextAssetPath(name), typeof(TextAsset));
        }
        private void AddReferenceUIPrefab<T>(ref T target) where T : Object
        {
            target = (T)UnityEditor.AssetDatabase.LoadAssetAtPath(GetUIPrefabPath(typeof(T).Name), typeof(T));
        }
        private string GetTextAssetPath(string name)
        {
            return $"Packages/com.makui.dt-framework/Content/Configs/{name}.txt";
        }
        private string GetUIPrefabPath(string name)
        {
            return $"Packages/com.makui.dt-framework/Content/UI/UIForms/{name}.prefab";
        }
#endif
        //private void AddScriptableName()
        //{
        //    m_ScriptableNames = GetDirectoryAllAssetNames("Scriptables", ".asset");
        //}
        //private void AddDataTableName()
        //{
        //    m_DataTableNames = GetDirectoryAllAssetNames("DataTables", ".bytes");
        //}

        private List<string> GetDirectoryAllAssetNames(string dirName, string suffix)
        {
            List<string> assetNames = new List<string>();
            var dirPath = @$"{Application.dataPath}/GameMain/{dirName}";
            //判断某个文件夹是否存在，不存在就创建
            if (!Directory.Exists(dirPath))
            {
                Debug.LogError($"==> 不存在 {dirPath} 资产目录！");
                return null;
            }
            var direction = new DirectoryInfo(dirPath);//获取文件夹，exportPath是文件夹的路径
            FileInfo[] files = direction.GetFiles("*", SearchOption.TopDirectoryOnly);
            for (int i = 0; i < files.Length; i++)
            {
                //判断文件的后缀
                if (files[i].Name.EndsWith(suffix))
                {
                    var exefileName = files[i].Name;
                    exefileName = RemoveSuffix(exefileName);
                    assetNames.Add(exefileName);
                    Debug.Log("==> 获取文件：" + files[i].Name);
                }
            }
            return assetNames;
        }

        public static string RemoveSuffix(string fileName)
        {
            return fileName.Substring(0, fileName.LastIndexOf("."));
        }
    }
}


